import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * API - 全局通用 API
 * @date: 2018-6-20

 */
import request from "../../utils/request";
import { getEnvConfig } from "../../utils/iocUtils";
import { getCurrentOrganizationId, getCurrentRole, getResponse, isTenantRoleLevel } from "../../utils/utils";

var _getEnvConfig = getEnvConfig(),
    CPAAS_IAM = _getEnvConfig.CPAAS_IAM,
    CPAAS_PLATFORM = _getEnvConfig.CPAAS_PLATFORM,
    TOP_MENU_LABELS = _getEnvConfig.TOP_MENU_LABELS,
    TOP_MENU_UNION_LABEL = _getEnvConfig.TOP_MENU_UNION_LABEL; // primary 私有的api(组件, 页面无关的api)

/**
 * 查询菜单.
 * {CPAAS_IAM}/cpaas/v1/menus/tree
 * @param {object} query - 现在菜单会包含语言
 */


export function queryMenu() {
  return _queryMenu.apply(this, arguments);
}
/**
 * 查询平台语言国际化
 * {CPAAS_PLATFORM}/v1/{organizationId}/prompt/{language}
 * @param {Number} organizationId
 * @param {String} language
 * @param {String} promptKey - 这里只用到了 cpaas.common
 */

function _queryMenu() {
  _queryMenu = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var query,
        role,
        params,
        _args = arguments;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            query = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
            role = getCurrentRole();
            params = TOP_MENU_LABELS && TOP_MENU_LABELS !== 'undefined' ? _objectSpread(_objectSpread({}, query), {}, {
              roldId: role.id,
              labels: TOP_MENU_LABELS,
              unionLabel: TOP_MENU_UNION_LABEL === 'undefined' ? false : TOP_MENU_UNION_LABEL || false
            }) : _objectSpread(_objectSpread({}, query), {}, {
              roldId: role.id,
              unionLabel: TOP_MENU_UNION_LABEL === 'undefined' ? false : TOP_MENU_UNION_LABEL || false
            });
            return _context.abrupt("return", request("".concat(CPAAS_IAM, "/cpaas/v1/menus/tree"), {
              method: 'GET',
              query: params
            }));

          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _queryMenu.apply(this, arguments);
}

export function queryPromptLocale(_x, _x2, _x3) {
  return _queryPromptLocale.apply(this, arguments);
}
/**
 * 查询 LOV 配置.
 * {CPAAS_PLATFORM}/v1/lov-view/info
 * @param {Object} params 参数
 */

function _queryPromptLocale() {
  _queryPromptLocale = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(organizationId, language, promptKey) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/prompt/").concat(language, "?promptKey=").concat(promptKey)));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _queryPromptLocale.apply(this, arguments);
}

export function queryLov(_x4) {
  return _queryLov.apply(this, arguments);
}
/**
 * 查询 LOV 数据.
 * @param {string} url URL
 * @param {Object} params 参数
 */

function _queryLov() {
  _queryLov = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var res;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            res = request("".concat(CPAAS_PLATFORM, "/v1/").concat(isTenantRoleLevel() ? "".concat(getCurrentOrganizationId(), "/") : '', "lov-view/info"), {
              method: 'GET',
              query: params
            }); // FIXME: @WJC utils need fix
            // @ts-ignore

            return _context3.abrupt("return", getResponse(res));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _queryLov.apply(this, arguments);
}

export function queryLovData(_x5, _x6) {
  return _queryLovData.apply(this, arguments);
}
/**
 * 查询动态表格
 * @param {object} query
 * @returns {Promise<void>}
 */

function _queryLovData() {
  _queryLovData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(url, params) {
    var res;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            res = request(url, {
              query: params
            }); // FIXME: @WJC utils need fix
            // @ts-ignore

            return _context4.abrupt("return", getResponse(res));

          case 2:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _queryLovData.apply(this, arguments);
}

export function queryUiTables() {
  return _queryUiTables.apply(this, arguments);
} // primary 私有的api(组件, 页面无关的api)

function _queryUiTables() {
  _queryUiTables = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
    var query,
        organizationId,
        _args5 = arguments;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            query = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : {};
            organizationId = getCurrentOrganizationId();
            return _context5.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(organizationId, "/ui-table"), {
              method: 'GET',
              query: query
            }));

          case 3:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _queryUiTables.apply(this, arguments);
}